home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / xobbs.arc / xofwd.c < prev    next >
C/C++ Source or Header  |  1989-05-03  |  18KB  |  516 lines

  1. /* XOFWD.C Mail forwarding routines for XOBBS. Jim Durham, W2XO 4-7-89 */
  2. /* Version 1.3 */
  3. /* Code released to the amateur radio community */
  4.  
  5. #include "xobbs.h"
  6.  
  7. char bbsname[15][7];                /*bbs name array*/
  8. char *connectline;                              /*pointer to connect line*/
  9. char *recvline[5];                             /*pointer to reply lines*/
  10. char *sendline[5];                              /*pointer to send lines*/
  11. int bbsindex;                                   /*number of bbs we are talking*/
  12. char device[5];
  13. int flag;
  14. int bbscount=0;                                 /*how many bbses in array*/
  15. jmp_buf env;                    /* setjmp() environment*/
  16. int sigcatch();
  17. int fd;
  18. char bbscall[7];
  19. int numdigis;
  20.     char whotocon[7];
  21.     char digi[5][7];                   /*digipeater strings*/
  22.     int sendindex,recvindex;
  23.  
  24. confwd()        /* connect to bbses and forward whatever goes to them*/
  25. {
  26.     int gotonext();
  27.     int callmatch;
  28.  
  29.     getnames();                /*get the names to forward to*/
  30.  
  31.     if((fd=open(fwdfile,O_RDONLY)) < 1){ /*open the forwarding file*/
  32.         perror("confwd: Can't open forwarding file");
  33.         return ;
  34.     }
  35.     for(;;){                /*main forwarding loop*/
  36.         nullsndrec();    /*null out the send and receive line pointer arrays*/
  37.  
  38.         if(getfwdinfo() == 1)              /*get forwarding info for one bbs */
  39.             break;                         /*break if no more to do from file*/
  40.  
  41.     callmatch = false;            /*preset call match off*/
  42.         for(bbsindex=0;bbsindex < 15; bbsindex++){  /*let see if we have mail for this one*/
  43.             if(!strcmp(bbsname[bbsindex],bbscall)){  /*does call match?*/
  44.         callmatch = true;
  45.                 break;
  46.             }
  47.         }
  48.         if(callmatch == false)            /*keep looping if no mail for */
  49.             continue;                /*  this bbs call gotten from file*/
  50. #ifdef DEBUG
  51.     printf("bbs:found bbs match:beginning connect\n");
  52. #endif
  53.         if(sendcon() == 1){                 /*unsuccessful connect*/
  54.             goto next;
  55.         }
  56.         sendindex=0;                        /*if all ok to this point*/
  57.         recvindex=0;                        /*set send & recv indexes to 0*/
  58.         signal(SIGUSR1,gotonext);
  59.     if(setjmp(env) == -1){              /*disconnect signal received from*/
  60. #ifdef DEBUG                                /* the NET process */
  61.             printf("bbs:signal SIGUSR received\n");
  62. #endif
  63.         goto next;
  64.     }
  65.         conbbs();                          /*continue connection to bbs*/
  66. #ifdef DEBUG
  67.     printf("bbs:successful connection\n");        
  68. #endif
  69. next:
  70. #ifdef DEBUG
  71.         printf("bbs:sending N\n");
  72. #endif
  73.         sprintf(prinbuf,"N\0");    /* tell NET to goto next con*/
  74.         cprinout();                    /*send control message*/
  75.         signal(SIGUSR1,SIG_IGN);
  76.     do{                  /* wait for confirmation of 'goto next' from NET*/
  77. #ifdef DEBUG
  78.         printf("calling cgetline , looking for N from NET\n");
  79. #endif
  80.             cgetline();
  81. #ifdef DEBUG
  82.         printf("bbs:got this from cgetline call:%s\n",inline);
  83. #endif
  84.         }while(inline[0] != 'N');
  85. #ifdef DEBUG
  86.         printf("bbs:N received from net, going to next\n");
  87. #endif
  88.     }/*for, which is exited by break above */
  89. #ifdef DEBUG
  90.     printf("bbs:sending K\n");
  91. #endif
  92.     sprintf(prinbuf,"K\0");        /*done with all forwarding*/
  93.     cprinout();                /*so kill bbs process*/
  94.     for(;;);                /*wait for kill*/
  95. } /* end of routine*/
  96.  
  97.  
  98. dofwd()
  99. {
  100.  
  101.     FILE *fp;
  102.     char s[30];
  103.     char hdr[128];
  104.     char buf[128];
  105.  
  106.     int ic;
  107.     int i,gotbytes;
  108.     struct msghdr mhdr;
  109.     int fd2;
  110.     char *cp;
  111.             
  112.     sprintf(prinbuf,"ls %s | fgrep %s",fwddir,user.call); /*scan forwarding dir*/
  113.     fp=popen(prinbuf,"r");
  114.     for(;;){
  115.         i=0;
  116.         while(((ic = getc(fp)) != '\n') && (ic != EOF)) /*read file names*/
  117.             s[i++] = ic & 0x7f;
  118.         if(ic == EOF) break;                /*break loop if EOF*/
  119.         s[i] = '\0';                  /*terminate filename*/
  120.  
  121.         sprintf(prinbuf,"%s%s",fwddir,s);       /*file name in s*/
  122.  
  123.         if((fd2=open(prinbuf,O_RDONLY)) < 0){
  124.             sprintf(prinbuf,"Cant open message file no.%s\n",s);
  125.         perror(prinbuf);
  126.             continue;        /*shouldn't happen*/
  127.         }
  128.         else
  129.         {
  130.             read(fd2,hdr,86);
  131.             conhdr(hdr,&mhdr);          /*convert header to structure*/
  132.  
  133.             sprintf(buf,"S");        /*start with the "S" command*/
  134.  
  135.             if(strlen(mhdr.type) > 0){        /*if there is a type, send it*/
  136.             sprintf(prinbuf,"%c",mhdr.type);/* as the option to s command*/
  137.             strcat(buf,prinbuf);
  138.             }
  139.  
  140.             sprintf(prinbuf," %s ",mhdr.tocall);    /*append 'to' call*/
  141.             strcat(buf,prinbuf);
  142.  
  143.             if(mhdr.atbbs[0] != '\0'){            /*append '@bbs' */
  144.                 sprintf(prinbuf,"@ %s ",mhdr.atbbs);
  145.             strcat(buf,prinbuf);
  146.             }
  147.  
  148.             sprintf(prinbuf,"< %s",mhdr.fromcall);    /*append 'fromcall' */
  149.         strcat(buf,prinbuf);
  150.  
  151.             if(mhdr.bid[0] != '\0')
  152.                sprintf(prinbuf," $%s\n",mhdr.bid);    /*append BID if any*/
  153.             else
  154.                 sprintf(prinbuf,"\n\0");        /* else, just NL */
  155.             strcat(buf,prinbuf);
  156.  
  157.             strcpy(prinbuf,buf);            /* copy to output*/
  158.             prinout(FLUSH);
  159.         
  160.     /*now we have sent the message header*/
  161.     /* see if it is OK */
  162.  
  163.             getline(0);                    /* get OK or NO */
  164.             if(revfwd){                       /* if we dont get an OK, go to next */
  165.                 if(inline[0] != 'O'){
  166.         goto skip;              /* if not OK, consider it done*/
  167.                 }
  168.             }
  169.             sprintf(prinbuf,"%s\n",mhdr.title);        /*send the title*/
  170.             if(revfwd)
  171.                 prinout(NOFLUSH);
  172.             else{
  173.         prinout(FLUSH);
  174.                 getline(0);
  175.             }
  176.  
  177.  
  178.     /*make the path line*/
  179.             sprintf(prinbuf,"R:%s/%sz @:%s %s   O:%s\n",mhdr.dat,mhdr.tim,my.call,my.qth,mhdr.fromcall);    
  180.             prinout(NOFLUSH);
  181.     
  182.             while(gotbytes = read(fd2,prinbuf,80)){        /*send the file*/
  183.                 prinbuf[gotbytes] = '\0';
  184.                 cp = prinbuf;
  185.                 while(*cp){                                 /*change LF to CR*/
  186.                     if(*cp == '\n')
  187.                         *cp = '\r';
  188.                     cp++;
  189.                 }
  190.                 prinout(NOFLUSH);
  191.             }
  192.  
  193.             prinbuf[0] = 0x1a;
  194.             prinbuf[1] = '\r';
  195.             prinbuf[2] = '\0';
  196.             prinout(FLUSH);
  197.             close(fd2);            /* close the file(tidy, aren't we?) */
  198.  
  199. skip:        waitprompt();        /* wait for prompt from remote bbs*/
  200.  
  201.             sprintf(prinbuf,"%s%s",fwddir,s);       /*file name in s*/
  202.             unlink(prinbuf);
  203.         }/*end of else-file-opened-OK */
  204.     }   /*end of for loop*/
  205.     pclose(fp);
  206.     if(fwding){
  207.        return;
  208.     }
  209.     sndupkil();         /* and kill bbs process */
  210. }
  211.  
  212. waitprompt()            /*wait for a prompt*/
  213. {
  214.     do{
  215.         getline(0);
  216.     }while(inline[strlen(inline)-1] != '>');
  217. }
  218.  
  219. /*GETNAMES IS USED IN FORWARDING INITIATED BY *THIS* BBS*/
  220.  
  221. getnames()                 /*fill bbs name array*/
  222. {                          /* containing names of bbses to*/
  223.                            /* which to initiate fwding connects*/
  224.     int i,j,ic;
  225.     FILE *fp;
  226.     char s[30];
  227.  
  228.                    
  229.     sprintf(prinbuf,"ls %s\n",fwddir);
  230.     fp=popen(prinbuf,"r");       /*open the pipe from ls*/
  231.  
  232.  
  233.     for(;;){                /*main loop*/
  234.         i=0;
  235.         while(((ic = getc(fp)) != '\n') && (ic != EOF))   /*read message numbers*/
  236.             s[i++] = ic & 0x7f;
  237.         if(ic == EOF) break;                            /*break loop if EOF*/
  238.         s[i] = '\0';                                  /*terminate filename*/
  239.  
  240.         for(j=0;j < strlen(s); j++){        /*shorten to just bbs name*/
  241.             if(s[j] == 't'){
  242.                 s[j]='\0';
  243.                 break;
  244.             }
  245.         }
  246.  
  247.         for(j=0;j < 15; j++){
  248.             if(!strcmp(bbsname[j],s))   /*if name is already there, exit loop*/
  249.                 break;
  250.  
  251.             if(bbsname[j][0]=='\0'){    /*if empty slot and name not used, */
  252.                 strcpy(bbsname[j],s);   /* put it in the slot*/
  253.                 bbscount++;         /* set the count of bbses up */
  254.                 break;
  255.             }
  256.         }    /*for j=0 to 14 */
  257.     }
  258.     pclose(fp);                    /*close the pipe*/
  259. }
  260.  
  261. devassign(dev)          /*assign device name according to dev char*/
  262.     char dev;
  263. {               switch(dev){
  264.                     case  'A':  strcpy(device,DEVICE1);
  265.                                 break;
  266. #ifdef DEVICE2
  267.                     case  'B':  strcpy(device,DEVICE2);
  268.                                 break;
  269. #endif
  270. #ifdef DEVICE3
  271.                     case  'C':  strcpy(device,DEVICE3);
  272.                                 break;
  273. #endif
  274. #ifdef DEVICE4
  275.                     case  'D':  strcpy(device,DEVICE4);
  276.                                 break;
  277. #endif
  278. #ifdef DEVICE5
  279.                     case 'E':  strcpy(device,DEVICE5);
  280.                    break;
  281. #endif
  282.                     default:    strcpy(device,DEVICE1);
  283.                 }
  284.                 
  285. }
  286.  
  287. gotonext()
  288. {
  289.     longjmp(env,-1);
  290. }
  291.  
  292. sigcatch(sig)
  293.    int sig;
  294. {
  295.     if(sig != 17) return;
  296.     signal(SIGUSR2,SIG_IGN);        /*ignore signal until next needed*/    
  297. }
  298.  
  299.  
  300. nullsndrec()
  301. {
  302.  
  303.                     /*null out the send and receive line pointer arrays*/
  304.         for(sendindex=0;sendindex < 5; sendindex++) 
  305.            sendline[sendindex]=NULL;
  306.         for(recvindex=0;recvindex < 5; recvindex++)
  307.            recvline[recvindex]=NULL;
  308. }
  309.  
  310. getfwdinfo()            /*get forwarding info from fwd.xo file*/
  311. {
  312.     char temp1[80],*p,*q;
  313.     char temp2[80];
  314.     int doneflag,i;
  315.  
  316.     doneflag = false;
  317.         do{
  318.             if(fgetline(fd,temp1) < 1) return 1;
  319.                              /*get a line from the forwarding file*/
  320.             switch(temp1[0]){
  321.                 case 'G':       /* if we get a 'G' line first */
  322.                         devassign(temp1[1]); /*assign device name by second char in line*/
  323.                         p=temp1;  /*point to the line*/
  324.                         while((*p != ' ') && (*p != '\0')) /*toggle up to the space*/
  325.                             p++;
  326.                         while((*p == ' ') && (*p != '0'))  /* and past any spaces*/
  327.                             p++;
  328.                         q=bbscall;    /*point to the bbscall string*/
  329.                         while((*q = *p) != ' ' && *p != '\0'){
  330.                             p++;
  331.                             q++;
  332.                         }
  333.                         *q='\0';    /*terminate bbscall*/
  334.                         i=0;        /*set up for first digi field*/
  335.             numdigis = 0;
  336.                         if(*p != '\0'){ /* digi fields are there*/
  337.                             do{
  338.                                 q=digi[i++];
  339.                                 while(*p == ' ')/*toggle past any spaces*/
  340.                             p++;
  341.                                 while((*q = *p) != ' ' && *p != '\0'){
  342.                         p++;
  343.                         q++;
  344.                                 }
  345.                                 *q='\0';
  346.                     }while(*p != '\0');
  347.                 }
  348.                         numdigis=i;
  349.                         doneflag=true;    /* we're all done, no netrom stuff */
  350.                         break;        /* exit the switch*/
  351.                 case 'C':       /*this is a connect line first*/
  352.                         p = &temp1[3];
  353.                         q = whotocon;
  354.                         while((*q = *p) != ' ' && *p != '\0'){
  355.                             p++;
  356.                             q++;
  357.                         }
  358.                         *q='\0';
  359.             numdigis = 0;
  360.                     sendindex=recvindex=0;    /*initialize send and recv indexes*/
  361.                         do{            /*read up to the next 'G' line*/
  362.                             if((fgetline(fd,temp2)) < 1) /*get a line*/
  363.                                 return 1;
  364.                             switch(temp2[0]){
  365.                                 case 'S':   sendline[sendindex]=(char *)malloc(60);
  366.                                             strcpy(sendline[sendindex++],&temp2[1]);/*copy sendline*/
  367.                                             break;
  368.                                 case 'R':   recvline[recvindex]=(char *)malloc(60);
  369.                                             strcpy(recvline[recvindex++],&temp2[1]);/*copy sendline*/
  370.                                             break;
  371.                                 case 'G':   
  372.                                             devassign(temp2[1]); /*assign device name*/
  373.                                             p=temp2;  /*point to the line*/
  374.                                             while(*p++ != ' ');/*toggle up to the space*/
  375.                                             rdfield(p,bbscall);/*get bbs call from line */
  376.                                             doneflag=true;
  377.                                             do{
  378.                                                 if(fgetline(fd,temp2) < 1)
  379.                                                     return 1;
  380.                                             }while(temp2[0] != '*');
  381.                                             break;
  382.                                 default:    break;
  383.                             } /*switch on temp2[1] */
  384.                         }while((temp1[0] != 'G') && (!doneflag));   /*terminate when we get a G line*/
  385.                 default:    break;
  386.             } /*switch on temp1[1] */
  387.         }while(!doneflag);                      /*done for this bbs */
  388.         return 0;        /*successful bbs read*/
  389. }
  390.  
  391.  
  392. conbbs()
  393. {
  394.     /* complete the connection to bbs that we are*/
  395.     /* going to forward to */
  396.  
  397.     for(;;){
  398.         if(sendline[sendindex]==NULL)            /* Any more to send?*/
  399.             break;
  400.         sprintf(prinbuf,"%s\n",sendline[sendindex++]);/*send send line*/
  401.         prinout(FLUSH);
  402.         if(recvline[recvindex] == NULL)         /*if we need no more responses*/
  403.             break;
  404.         getline(1);          /*if we are expecting response*/
  405.         if(strcmp(inline,recvline[recvindex++])){/*error of some sort*/
  406. #ifdef DEBUG
  407.             printf("bbs:conbbs:proper response not received\n");
  408. #endif
  409.         return 1;
  410.     }
  411.  
  412.     } /*for(;;)*/
  413.  
  414.                         /* OK, we are done with the connect stuff*/
  415.  
  416.  
  417.     strcpy(user.call,bbscall);          /*make the current user the remote bbs*/
  418.     user.typ='B';                       /* and mark it as bbs */
  419.  
  420. #ifdef DEBUG
  421.     printf("Looking for prompt \n");
  422. #endif
  423.                                         /*look for prompt*/
  424.     do{
  425.         getline(1);      /*get a prompt from remote bbs*/
  426. #ifdef DEBUG
  427.     printf("%s\n",inline);
  428. #endif
  429.         if((inline[0] == '[')
  430.           && (inline[strlen(inline)-1] == ']')){
  431. #ifdef DEBUG
  432.     printf("SID Received\n");
  433. #endif
  434.             revfwd = true;     /*set reverse forward flag*/
  435.         }
  436.     }while(inline[strlen(inline)-1] != '>');
  437. #ifdef DEBUG
  438.     if(revfwd) printf("Sending our SID\n");
  439. #endif
  440.                 /* if reverse forwarding bbs is connected, send SID*/
  441.                 /*  and get second prompt*/
  442.     if(revfwd){
  443.         sprintf(prinbuf,"[XOBBS-V1.2-$]\n");/*send rev fwd sid*/
  444.         prinout(FLUSH);
  445.  
  446. #ifdef DEBUG
  447.     printf("Looking for a prompt again\n");
  448. #endif
  449.         do{
  450.             getline(1);
  451.         }while(inline[strlen(inline)-1] != '>');    /*get prompt*/
  452. #ifdef DEBUG
  453.     printf("Prompt received after  SID sent\n");
  454. #endif
  455.     }
  456.  
  457.  
  458.     dofwd();                        /*forward to the remote bbs*/
  459.                 /* now, if we are connected to reverse forwarding bbs, do so*/
  460.     if(revfwd){
  461.         fwding=false;            /*clear so bbs operates normally*/
  462.         sprintf(prinbuf,"F>\n");    /*send reverse foward prompt*/
  463.         prinout(FLUSH);
  464.                                     /*now, act like incoming bbs for a while*/
  465.         for(;;){
  466.             getline(0);                        /*get a line*/
  467.             if(matchn2("*** ",inline,4)){     /*check for *** Done*/
  468.                 fwding = true;
  469.                 break;
  470.             }
  471.             else{
  472.                 if(parse()){                    /*parse line*/
  473.                     docmd();                    /* do what is asked*/
  474.                     sprintf(prinbuf,">\n");     /*print a prompt*/
  475.                     prinout(FLUSH);
  476.                 }
  477.                 else{
  478.                     fwding = true;
  479.                     break;                      /*something screwed up*/
  480.                 }
  481.             }
  482.                                                 /* so drop the connection*/
  483.         }/*for*/
  484.         fwding = true;
  485.     }/*if reverse forward*/                    
  486.     return 0;
  487. }
  488.  
  489. sendcon()  /*send connect info to net */
  490. {
  491.     int i;
  492.  
  493.     sprintf(prinbuf,"%s",device);               /*device name*/
  494.     prinout(FLUSH);
  495.     sprintf(prinbuf,"%s",whotocon);             /*call letters*/
  496.     prinout(FLUSH);
  497.     sprintf(prinbuf,"%d",numdigis);             /*number of digis*/
  498.     prinout(FLUSH);
  499.     for(i=0;i<numdigis;i++){                    /*digi calls*/
  500.         sprintf(prinbuf,"%s",digi[i]);
  501.         prinout(FLUSH);
  502.     }
  503.     prinbuf[0] = 0x04;                          /*control D for termination*/
  504.     prinbuf[1] = '\0';
  505.     prinout(FLUSH);
  506.                 /* connect info now sent, get a line from the bbs */
  507.     getline(0);
  508.  
  509.     if(indexx("NOGOOD",inline) >= 0){          /*Busy messsage? */
  510.         bbsname[bbsindex][0]='\0';             /*no more for this bbs this time*/
  511.         return 1;                              /*return, no success */
  512.     }
  513.     return 0;                           /*successful connect*/
  514. }
  515.  
  516.